package com.oracle.controller;

import com.oracle.Service.impl.BookService;
import com.oracle.entity.ResultBean;
import com.oracle.entity.ShoppingCart;
import com.oracle.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;


@Controller
@RequestMapping("/shop")
public class ShoppingController {

    @Autowired
    private ShoppingCart shoppingCart;

    @Autowired
    private BookService bookService;

    @RequestMapping("/add")
    @ResponseBody
    public ResultBean add(Book book,Integer number, HttpSession session){
        ResultBean resultBean = null;
        try{
            if (book!=null){
                if (book.getBid()!=null&&book.getBid()!=0){
                    book = bookService.seleteBookbyId(book);
                    if (session.getAttribute("cart")==null){
                        session.setAttribute("cart",shoppingCart);
                    }
                    shoppingCart.add(book,number);
                    resultBean = ResultBean.success("添加成功", shoppingCart);
                }
            }
            return resultBean;
        }catch (Exception e){
            return ResultBean.fail("添加失败",null);
        }
    }

    @RequestMapping("/remove")
    @ResponseBody
    public ResultBean remove(Book book,HttpSession session){
        try{
            if (session.getAttribute("cart")==null){
                session.setAttribute("cart",shoppingCart);
            }
            shoppingCart.remove(book.getBid());
            return  ResultBean.success("删除成功", shoppingCart);
        }catch (Exception e){
            return  ResultBean.fail("删除失败", null);
        }
    }

    @RequestMapping(value = "/clear",method = RequestMethod.GET)
    @ResponseBody
    public ResultBean clear(HttpSession session){
        try{
            if (session.getAttribute("cart")==null){
                session.setAttribute("cart",shoppingCart);
            }
            shoppingCart.clear();
            return  ResultBean.success("清空成功", shoppingCart);
        }catch (Exception e){
            return  ResultBean.fail("清空失败", shoppingCart);
        }
    }

    @RequestMapping("/changeNum")
    @ResponseBody
    public ResultBean changeNum(Integer bid,Integer num,HttpSession session){
        try{
            if (session.getAttribute("cart")==null){
                session.setAttribute("cart",shoppingCart);
            }
            shoppingCart.changeNum(bid,num);
            System.out.println(shoppingCart.getMap().get(bid));
            return  ResultBean.success("修改成功", shoppingCart);
        }catch (Exception e){
            return  ResultBean.fail("修改失败", null);
        }
    }

    @RequestMapping("/overOrder")
    public String overOrder(HttpSession session){
        try {
            //清除购物车中的商品，并且返回到购物车页
            if (session.getAttribute("cart")==null){
                session.setAttribute("cart",shoppingCart);
            }
            shoppingCart.clear();
            return "redirect:/user/front/cart";
        }catch (Exception e){
            return "redirect:/user/front/cart";
        }
    }
}
